<template>
  <div class="add-permission-container">
    <!-- 页面标题区域 -->
    <div class="page-header">
      <div class="header-content">
        <div class="title-section">
          <el-icon class="title-icon" size="32">
            <Key />
          </el-icon>
          <h1 class="page-title">添加权限</h1>
        </div>
        <p class="page-description">创建新的系统权限并配置相关参数</p>
      </div>
    </div>

    <!-- 表单卡片容器 -->
    <div class="form-container">
      <el-card class="form-card" shadow="hover">
        <template #header>
          <div class="card-header">
            <el-icon class="card-icon" size="20">
              <Edit />
            </el-icon>
            <span class="card-title">权限信息</span>
          </div>
        </template>

        <el-form ref="ruleFormRef" class="permission-form" :model="ruleForm" :rules="rules" label-width="100px"
          label-position="left">
          <!-- 权限名称输入 -->
          <el-form-item label="权限名称" prop="permissonName" class="form-item">
            <el-input v-model="ruleForm.permissonName" placeholder="请输入权限名称" clearable class="form-input">
              <template #prefix>
                <el-icon class="input-icon">
                  <Key />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <!-- 权限值输入 -->
          <el-form-item label="权限值" prop="permissonValue" class="form-item">
            <el-input v-model="ruleForm.permissonValue" placeholder="请输入权限值" clearable class="form-input">
              <template #prefix>
                <el-icon class="input-icon">
                  <Link />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <!-- 父级ID输入 -->
          <el-form-item label="父级ID" prop="perseId" class="form-item">
            <el-input v-model="ruleForm.perseId" placeholder="请输入父级ID" clearable class="form-input">
              <template #prefix>
                <el-icon class="input-icon">
                  <Folder />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <!-- 按钮区域 -->
          <el-form-item class="button-section">
            <div class="button-group">
              <el-button type="primary" size="large" :loading="isSubmitting" @click="submitForm(ruleFormRef)"
                class="submit-btn">
                <el-icon class="btn-icon">
                  <Check />
                </el-icon>
                添加权限
              </el-button>
              <el-button size="large" @click="resetForm(ruleFormRef)" class="reset-btn">
                <el-icon class="btn-icon">
                  <Refresh />
                </el-icon>
                重置表单
              </el-button>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { read, write } from '@/Until/axiosHepler'
import { ElMessage } from 'element-plus'
import {
  Key,
  Edit,
  Link,
  Folder,
  Check,
  Refresh
} from '@element-plus/icons-vue'
interface RuleForm {
  "permissonName": string,
  "permissonValue": string,
  "perseId": string
}

const ruleFormRef = ref<FormInstance>()
const isSubmitting = ref(false)
const ruleForm = reactive<RuleForm>({
  "permissonName": "",
  "permissonValue": "",
  "perseId": ""
})

const rules = reactive<FormRules<RuleForm>>({
  permissonName: [
    { required: true, message: '权限名不能为空', trigger: 'blur' },

  ],
  permissonValue: [
    {
      required: true,
      message: '权限值不能为空',
      trigger: 'blur',
    },
  ],
  perseId: [
    {
      required: true,
      message: '父级Id不能为空',
      trigger: 'blur',
    },
  ],

})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return

  // 基本验证
  if (!ruleForm.permissonName.trim()) {
    ElMessage.warning('请输入权限名称')
    return
  }
  if (!ruleForm.permissonValue.trim()) {
    ElMessage.warning('请输入权限值')
    return
  }
  if (!ruleForm.perseId.trim()) {
    ElMessage.warning('请输入父级ID')
    return
  }

  try {
    await formEl.validate(async (valid, fields) => {
      if (valid) {
        isSubmitting.value = true
        try {
          const res = await write.post("api/Manger/AddPermisson", ruleForm)
          if (res.data.code == 200) {
            ElMessage.success("添加权限成功")
            resetForm(formEl)
          } else {
            ElMessage.error(res.data.message || "添加权限失败")
          }
        } catch (error) {
          console.error('提交失败:', error)
          ElMessage.error("网络错误，请稍后重试")
        } finally {
          isSubmitting.value = false
        }
      } else {
        ElMessage.warning("请检查表单填写是否完整")
      }
    })
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>
.add-permission-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.page-header {
  width: 100%;
  max-width: 800px;
  margin-bottom: 30px;
  animation: fadeInDown 0.6s ease-out;
}

.header-content {
  text-align: center;
  color: white;
}

.title-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 10px;
}

.title-icon {
  color: #ffd700;
  animation: pulse 2s infinite;
}

.page-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-description {
  font-size: 1.1rem;
  opacity: 0.9;
  margin: 0;
  font-weight: 300;
}

.form-container {
  width: 100%;
  max-width: 800px;
  animation: fadeInUp 0.8s ease-out;
}

.form-card {
  border-radius: 16px;
  border: none;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #2c3e50;
}

.card-icon {
  color: #667eea;
}

.card-title {
  font-size: 1.2rem;
}

.permission-form {
  padding: 20px 0;
}

.form-item {
  margin-bottom: 24px;
}

.form-input {
  transition: all 0.3s ease;
}

.form-input:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}

.input-icon {
  color: #667eea;
  transition: color 0.3s ease;
}

.form-input:focus-within .input-icon {
  color: #764ba2;
}

.button-section {
  margin-top: 40px;
  margin-bottom: 0;
}

.button-group {
  display: flex;
  gap: 16px;
  justify-content: center;
  flex-wrap: wrap;
}

.submit-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  border: none;
  border-radius: 8px;
  padding: 12px 32px;
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.submit-btn:active {
  transform: translateY(0);
}

.reset-btn {
  background: #f8f9fa;
  border: 2px solid #e9ecef;
  color: #6c757d;
  border-radius: 8px;
  padding: 12px 32px;
  font-weight: 600;
  transition: all 0.3s ease;
}

.reset-btn:hover {
  background: #e9ecef;
  border-color: #dee2e6;
  color: #495057;
  transform: translateY(-2px);
}

.btn-icon {
  margin-right: 6px;
}

/* 动画效果 */
@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {

  0%,
  100% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .add-permission-container {
    padding: 15px;
  }

  .page-title {
    font-size: 2rem;
  }

  .page-description {
    font-size: 1rem;
  }

  .form-card {
    margin: 0;
  }

  .button-group {
    flex-direction: column;
    align-items: center;
  }

  .submit-btn,
  .reset-btn {
    width: 100%;
    max-width: 300px;
  }
}

@media (max-width: 480px) {
  .title-section {
    flex-direction: column;
    gap: 8px;
  }

  .page-title {
    font-size: 1.8rem;
  }
}
</style>
